/**
 *  Triggers.Evt.afterinsert  Triggers.Evt.afterupdate
 */
public without sharing class OpportunityHandler 
{
	/*Set<Id> ownerIdSet = new Set<Id>();
	Set<Id> oIdSet =new Set<Id>();
	Map<Id,Opportunity> oMap = new Map<Id,Opportunity>();
	Map<Id,Account> accMap;
	Set<Id> aidSet = new Set<Id>();
    Set<Id> countryIdSet = new Set<Id>();
    Map<Id,Id> ocunMap = new Map<Id,Id>();
	Map<Id,Opportunity> beforeoppMap = new Map<Id,Opportunity>();
    Static String marker ='';
    Map<Id,RecordType> retMap;

    public void handle() {
    	for (Opportunity opp :(List<Opportunity>)Trigger.new){
          retMap = new Map<Id,RecordType>([select Id, Name, DeveloperName from RecordType]);
            if(Trigger.isInsert && Trigger.isAfter){
                if(!retMap.isEmpty()){
                    RecordType rt = retMap.get(opp.RecordTypeId);
            System.debug('ddddddd：'+opp.RecordTypeName__c);

                    if(rt.DeveloperName == 'OverseaDearlerOpportunity'){
                        if(opp.Country__c != null){
                            countryIdSet.add(opp.Country__c);  
                        }
                        ocunMap.put(opp.id, opp.Country__c);
                    }
                }
            }
            if(Trigger.isUpdate && Trigger.isAfter){

                System.debug('新增修改后：');
                Opportunity oldo = (Opportunity)Trigger.oldMap.get(opp.Id);
                RecordType rt = retMap.get(oldo.RecordTypeId);
                if(oldo.Country__c != opp.Country__c && oldo.RecordTypeName__c == 'OverseaDearlerOpportunity'){
                    if(opp.Country__c != null){
                        countryIdSet.add(opp.Country__c);  
                    }
                    ocunMap.put(opp.id, opp.Country__c);
                }
            }
    	}
        if(!ocunMap.isEmpty()){
            cloneCountryInfo();
        }
    }
    */
    public static void updateStageTime(List<Opportunity> list_new , Map<Id,Opportunity>oldMap){
        List<Opportunity> uList = new List<Opportunity>();
        for(Opportunity opp:list_new){
            Opportunity old = oldMap.get(opp.Id);
            if(old.StageName != '立项' && opp.StageName == '立项' )opp.ProjectEstablishment__c = System.now();
            if(old.StageName != '招标' && opp.StageName == '招标' )opp.BiddingPhase__c = System.now();

            if(old.StageName != '方案谈判' && opp.StageName == '方案谈判' )opp.ProgrammeNegotiationTime__c = System.now();

            if(old.StageName != '合同签订' && opp.StageName == '合同签订' ) opp.ContractSigningTime__c = System.now();      
            if(old.StageName != '失败' && opp.StageName == '失败' )opp.LostTime__c = System.now();

            if(old.StageName != '市场沟通'  && opp.StageName == '市场沟通') opp.MarketCommunicationTime__c = System.now();
            if(old.StageName != '合作细节谈判' && opp.StageName == '合作细节谈判') opp.NegotiationTime__c = System.now();
            if(old.StageName != '提起合作确认' && opp.StageName == '提起合作确认')opp.InitiatingCooperationTime__c = System.now();
            if(old.StageName != '市场管理' && opp.StageName == '市场管理')opp.MarketManagementTime__c = System.now();

        }

    }
    /*
    //clone国家及子项信息
    public void cloneCountryInfo(){
        //清空业务机会下的国家
        Set<Id> oSet = ocunMap.keySet();//获取业务机会ID
        System.debug('@oSet: '+oSet);
        List<Country__c> delcList = [select Id,Name from Country__c  where Opportunity__c in: oSet];
        if(!delcList.isEmpty()){
            System.debug('@delcList: '+delcList);
            delete delcList;
        }
        //跟进选中的国家进行链clone
        if( !countryIdSet.isEmpty() ){                 
            System.debug('@countryIdSet: '+countryIdSet);
            //查找并返回复制的map
            Country__c cobj = new Country__c();
            Map<Id,SObject> anewMap = cloneSObject(cobj,'Id',countryIdSet);
            //给复制的国家概况的业务机会赋值   
            List<SObject> newCounList = new List<SObject>();   
            //获取记录类型ID
            List<RecordType> counRecordtype = [select Id, Name from RecordType where DeveloperName = 'NowCountryArchive'];
            for(Id oid : oSet){
                Id cunid = ocunMap.get(oid);//根据业务机会获取国家的ID
                SObject c = anewMap.get(cunid); //获取原国家的克隆记录
                c.put('Opportunity__c',oid);     //给业务机会赋值
                if(!counRecordtype.isEmpty()){
                    c.put('RecordTypeId', counRecordtype[0].Id);  //赋值记录类型
                }
                c.put('Id', null);  //清空克隆数据ID
                c.put('OldCountry__c',cunid); //保存原版ID
                newCounList.add(c);
            }
            System.debug('@赋值后anewMap: '+anewMap);
            //插入copy的国家
            insert newCounList;
            System.debug('@newCounList: ' + newCounList);

            Map<Id,Id> onIdMap = new Map<Id,Id>();
            for(SObject c : newCounList){
                onIdMap.put((Id)c.get('OldCountry__c'),(Id)c.get('Id'));  //===Map<oldCountryId,newCountryId>
            }

            //医疗整体市场
            MedicalGrade__c cmobj = new MedicalGrade__c();
            Map<Id,SObject> cmnewMap = cloneSObject(cmobj,'Country__c',onIdMap.keySet());
            System.debug('@cmnewMap: '+cmnewMap);      
            Map<Id,Id> aonIdMap = insertCountryChildren(onIdMap,cmnewMap,'Country__c');

            //IVD整体市场
            IVDWholeMarket__c vmobj = new IVDWholeMarket__c();
            Map<Id,SObject> vmnewMap = cloneSObject(vmobj,'Country__c',onIdMap.keySet());
            System.debug('@vmnewMap: '+vmnewMap);
            Map<Id,Id> vmIdMap = insertCountryChildren(onIdMap,vmnewMap,'Country__c');

            //IVD各产品线市场
            IVDProductLineMarket__c pmobj = new IVDProductLineMarket__c();
            Map<Id,SObject> pmnewMap = cloneSObject(pmobj,'Country__c',onIdMap.keySet());
            System.debug('@pmnewMap: '+pmnewMap);
            Map<Id,Id> pmIdMap = insertCountryChildren(onIdMap,pmnewMap,'Country__c');

        }
    }

    //根据对象条件检索，并返回clone的Map
    public Map<Id,SObject> cloneSObject(SObject aobj,String termStr,Set<Id> termId){
        Schema.DescribeSobjectResult m = aobj.getSObjectType().getDescribe();  
        //aobj.getSObjectType();获取对象API
        Map<String, Object> fieldMap = m.fields.getMap();
        System.debug('@cloneSObject\n fieldMap:'+fieldMap);
        //获取对象字段Map
        List<String> termIdStr = new List<String>();

        for(Id aid : termId){
            String idstr = '\''+aid+'\'';
            termIdStr.add(idstr);
        }

        System.debug('@termIdStr: '+termIdStr);
        String sql = 'select ';
        String fields = '';
        for (String field : fieldMap.keySet()) {
            if (fields.length() > 0) {
                fields += ', '; 
            }
            fields += field;
        }
        sql += fields;
        sql += ' from ' + aobj.getSObjectType() + ' where '+termStr+' in ' + termIdStr+'';
        System.debug('@cloneSObject\nsql: '+sql);
        List<SObject> mc = Database.query(sql);
        System.debug('@cloneSObject\nmc: '+mc);
        List<SObject> newmc = mc.deepClone(true,false);//保留旧ID，清除创建人等字段
        System.debug('@cloneSObject\nnewmc: '+newmc);

        Map<Id,SObject> aMap = new Map<Id,SObject>();
        for(SObject s : newmc){
            aMap.put(s.Id,s);
        }
        System.debug('@cloneSObject\naMap: '+aMap);
        return aMap;
    }

     public Map<Id,Id> insertCountryChildren(Map<Id,Id> onIdMap,Map<Id,SObject> aMap ,String aField){
        Map<Id,Id> anewIdMap = new Map<Id,Id>();
        System.debug('is begin : \n onIdMap: '+onIdMap+'\n aMap: '+aMap);
        if(!onIdMap.isEmpty() && !aMap.isEmpty()){
            List<SObject> newList  = new List<SObject>();
            for(Id aid : aMap.keySet()){
                SObject s = aMap.get(aid);
                //Id oldID = (Id)s.get('Id');
                s.put(aField,onIdMap.get((Id)s.get(aField)));  //保存新的父级ID
               // s.put('OriginalId__c',aid); //保存Copy版本ID
                s.put('Id', null); //清空自己ID
                newList.add(s);
            }
            insert newList;
            System.debug('@afterinsert :' + newList);

            for(SObject s : newList){
                anewIdMap.put((Id)s.get('OriginalId__c'),(Id)s.get('Id'));
            }
            System.debug('@changed anewIdMap :' + anewIdMap);
        }
        return anewIdMap;
    } */
}